package middleware

import (
	"fmt"
	"golang_start/common"
	"golang_start/config/redis"

	"github.com/gin-gonic/gin"
)

func LoginMiddleware(c *gin.Context) {
	// cookei, err := c.Cookie("token")
	fmt.Println("执行局部中间件")
	token := c.GetHeader("token")
	uid := ""
	if token != "" {
		uid = redis.Get(token)
	}
	if uid == "" {
		c.AbortWithStatusJSON(common.SUCCESS_CODE, gin.H{
			"code":    common.FAIL_CODE,
			"message": "请先登录",
			"data":    "Request Denied",
		})
	}
	c.Set("uid", uid)
	c.Next()
}
